package com.yuanlrc.school.controller;


import com.github.pagehelper.PageInfo;
import com.yuanlrc.school.entity.DebateDB;
import com.yuanlrc.school.service.DebateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/debate")
public class DebateController {
    @Autowired
    private DebateService debateService;

    /**
     * 进入发布文章页面
     * @return
     */
    @RequestMapping("addDebate")
    public ModelAndView addDebate(String did,ModelAndView modelAndView) {
        modelAndView.addObject("did",did);
        modelAndView.setViewName("view/discussion/addDebate");
        return modelAndView;
    }

    /**
     * 进入讨论管理页面
     * @return
     */
    @RequestMapping("debateControl")
    public ModelAndView debateControl(String did,ModelAndView modelAndView) {
        modelAndView.addObject("did",did);
        modelAndView.setViewName("view/discussion/debateControl");
        return modelAndView;
    }


    /**
     * 发布讨论
     */
    @RequestMapping("addingDebate")
    @ResponseBody
    public Object addingDebate(DebateDB debateDB){
        int r = debateService.addDebate(debateDB);
        if(r==1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取讨论列表
     */
    @RequestMapping("findallDebate")
    @ResponseBody
    public Object findallDebate(DebateDB debateDB, Integer page, Integer limit){
//        PageHelper.startPage(page, limit);
        List<DebateDB> listAll = debateService.findAllDebate(debateDB);
        PageInfo pageInfo = new PageInfo(listAll);
        Map<String, Object> newsData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        newsData.put("code", 0);
        newsData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        newsData.put("count", pageInfo.getTotal());
        //将分页后的数据返回（每页要显示的数据）
        newsData.put("data", pageInfo.getList());
        return newsData;
    }

    /**
     * 删除讨论
     * */
    @RequestMapping("deleteDebate")
    @ResponseBody
    public Object deleteDebate(Integer deid){
        int r = debateService.deleteDebate(deid);
        if(r==1){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 编辑讨论
     */
    @RequestMapping("editingDebate")
    @ResponseBody
    public Object editDebate(DebateDB debateDB){
        int r = debateService.editDebate(debateDB);
        if(r==1){
            return true;
        }else{
            return false;
        }
    }

}
